<template>
	<view>
		<!-- 搜索栏 -->
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
		</view>
		
		<!-- 搜索建议列表 -->
		<view class="sugg-list" v-if="searchResults.length !== 0">
			<view class="sugg-item" v-for="(item,index) in searchResults" :key="index" @click="gotoDetail(item)">
				<view class="hosp-name">
					{{item.name}}
				</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		
		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<!-- 标题 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clean"></uni-icons>
			</view>
			<!-- 历史列表 -->
			<view class="history-list">
				<uni-tag :text="item" v-for="(item,index) in histories" :key="index" @click="gotoHospList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//延时器
				timer:null,
				//关键词
				kw:'',
				//搜索结果列表
				searchResults:[],
				//历史记录
				historyList:[]
			};
		},
		
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		
		computed:{
			histories(){
				return [...this.historyList].reverse()
			}
		},
		methods:{
			//input输入函数
			input(e){
				clearTimeout(this.timer)
				this.timer = setTimeout(()=>{
					this.kw = e
					this.getSearchList()
				},500)
			},
			
			//根据输入关键字获取搜索结果
			getSearchList(){
				//判断搜索关键词为空
				if(this.kw.trim() === ''){
					this.searchResults = []
					return
				}
				
				uni.request({
					url:'http://192.168.43.224:9001/admin/hospinfo/searchByLike/'+this.kw,
					method:'GET',
					success: (res) => {
						this.searchResults = res.data.data
						console.log(this.searchResults)
						this.saveHistoryKeyWord()
					}
				})
			},
			
			//跳转详情页
			gotoDetail(item){
				console.log(item.id)
				uni.navigateTo({
					url:'/subpkg/hosp_detail/hosp_detail?id='+item.id
				})
			},
			
			//保存历史关键字
			saveHistoryKeyWord(){
				// this.historyList.unshift(this.kw)
				const set = new Set(this.historyList)
				set.delete(this.kw)
				set.add(this.kw)
				this.historyList = Array.from(set)
				
				//对搜索历史数据进行持久化存储
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			
			//清空历史记录
			clean(){
				this.historyList = []
				uni.clearStorageSync('kw')
			},
			
			gotoHospList(item){
				console.log('历史记录项触发')
				uni.reLaunch({
					url:'/pages/home/home?name='+item	
				})
			}
		}
	}
</script>

<style lang="scss">
.search-box{
	position: sticky;
	top: 0;
	z-index: 999;
}

.sugg-list{
	padding: 0px 10px;
	.sugg-item{
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 12px;
		padding: 13px 0px;
		border-bottom: 1px solid #efefef;
		.hosp-name{
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
		}
	}
}

.history-box{
	padding: 0px 5px;
	.history-title{
		display: flex;
		justify-content: space-between;
		height: 40px;
		align-items: center;
		font-size: 13px;
		border-bottom: 1px solid #efefef;
	}
	.history-list{
		display: flex;
		flex-wrap: wrap;
		.uni-tag{
			margin-top: 5px;
			margin-left: 5px;
		}
	}
}
</style>
